﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using Microsoft.Win32;
using System.IO;
using System.Security.Permissions;
using System.Security.AccessControl;

namespace MyMediaPlayer
{

    /// <summary>
    /// Interaction logic for Window_Preference.xaml
    /// </summary>
    public partial class Window_Preference : MetroWindow
    {

        public Window_Preference()
        {
            InitializeComponent();

        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string color = ((ComboBoxItem)cbo_Thems.SelectedItem).Content.ToString();
            Properties.Settings.Default.SelectedColor = color;
            Properties.Settings.Default.Save();
        }

        private void ToggleSwitchButton_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.IsEnableBorder = true;
            Properties.Settings.Default.Save();
        }

        private void tswbtn_Unchecked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.IsEnableBorder = false;
            Properties.Settings.Default.Save();
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            nmUpDown.Value = Properties.Settings.Default.volum;
            this.Topmost = (Properties.Settings.Default.IsTopMost) ? true : false;
            this.tswbtnTopMost.IsChecked = (Properties.Settings.Default.IsTopMost) ? true : false;
            tswbtn.IsChecked = Properties.Settings.Default.IsEnableBorder;
            cbo_Thems.Text = Properties.Settings.Default.SelectedColor;
            if (tswbtn.IsChecked == true)
            {
                this.EnableDWMDropShadow = false;
            }
            else
            {
                this.EnableDWMDropShadow = true;
            }
        }

        private void tswSetAsDefault_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.IsDefaultPlayer = true;
            Properties.Settings.Default.Save();
            //foreach (string ext in this.exts)
            //{
            //    //var v = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
            //    //v.Demand();
            //    //RegistryPermission per = new RegistryPermission(PermissionState.Unrestricted);
            //    //per.Demand();
            //    //RegistryKey currentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + ext, true);
            //    //currentUser = currentUser.OpenSubKey("UserChoice", true);

            //    try
            //    {
            //        string key = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + ext + "\\UserChoice";
            //        RegistryKey reg = Registry.CurrentUser.OpenSubKey(key);

            //        string gname = Environment.UserDomainName + @"\" + Environment.UserName;
            //        RegistrySecurity rs = new RegistrySecurity();
            //        rs.AddAccessRule(new RegistryAccessRule(gname, RegistryRights.WriteKey, AccessControlType.Allow));
            //        reg.SetAccessControl(rs);

            //        //Registry.SetValue(key, "Proid", Environment.CurrentDirectory + "\\" + "MyMediaPlay.exe");
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message);
            //    }

            //}
        }

        private void tswSetAsDefault_Unchecked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.IsDefaultPlayer = false;
            Properties.Settings.Default.Save();
        }

        private void Button_MouseMove(object sender, MouseEventArgs e)
        {
            btnRestart.Foreground = (Brush)System.Windows.Application.Current.Resources["AccentColorBrush"];
        }

        private void btnRestart_MouseLeave(object sender, MouseEventArgs e)
        {
            btnRestart.Foreground = Brushes.Snow;
        }

        private void btnRestart_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
            System.Windows.Forms.Application.Restart();
        }

        private void nmUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double?> e)
        {
            Properties.Settings.Default.volum = Convert.ToDouble(nmUpDown.Value);
            Properties.Settings.Default.Save();
        }

        private void tswbtnTopMost_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.IsTopMost = true;
            Properties.Settings.Default.Save();
            foreach (Window w in Application.Current.Windows)
            {
                if (w.Title == "Meepo Player") w.Topmost = true;
            }
            this.Topmost = true;
        }

        private void tswbtnTopMost_Unchecked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.IsTopMost = false;
            Properties.Settings.Default.Save();
            foreach (Window w in Application.Current.Windows)
            {
                if (w.Title == "Meepo Player") w.Topmost = false;
            }
            this.Topmost = true;
        }

    }
}
